// net_test.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "ActiveSocket.h"
#include "PassiveSocket.h"
#include <string>
#include <iostream>
#pragma comment(lib, "clsocket.lib")

using namespace std;

const char *response = "HTTP/1.1 200 OK\n"
	"Date: Thu, 11 Apr 2013 01:08:29 GMT\n"
	"Server: Apache/2.2.9 (Debian) DAV/2 SVN/1.5.1 PHP/5.2.6-1+lenny16 with Suhosin-Patch\n"
	"Vary: Accept-Encoding\n"
	"Content-Length: 450\n"
	"Connection: close\n"
	"Content-Type: text/plain\n\n"
	"==51.B3=A013A8=C5=A=A2415BC2B259B9D429AA3D08264757243D4959D972D6"
	".D60DDD2A94566259532=D.4214BBC3933D339C=761460=6=.1=A05.1C42=597"
	"18634C4=6B51=920=4.D27B.A..3A344B140B6C=067143976=2BA7D=75=B4=78"
	"4=CDD5C081AD.7BADB=623=524.79486C8767A=5D01933=90==C4.A763.275D1"
	"44DD5A436C=B=677850611C99.4.6D2351=7838043BB9AC64D1D5A=318.B2BB5"
	"=DD..B3235.0A=22310BB1AA.7B9AA150.51.14A.C42D934199D80AD041DB25D"
	"D0A9105A7.698DD72857.86CB3C..76D=.65352=23=81.7C.7196=5A0=8=\n";

int _tmain(int argc, _TCHAR* argv[])
{
	CPassiveSocket server;
	CActiveSocket *pSession;
	int retval;
	bool ret;
	uint8* pData = NULL;

	ret = server.Initialize();
	ret = server.Listen((uint8 *)"localhost", 80);
	while(1)
	{
		pSession = server.Accept();
		printf("session established.\n");
		retval = pSession->Receive(1024);
		if(retval < 0)
		{
			printf("receive error\n");
			goto exit;
		}
		pData = pSession->GetData();
		pData[retval] = '\0';
		printf("received: %s\n", pData);
		FILE *pFile = NULL;
		pFile = fopen("d:\\in.log", "wb");
		if(NULL == pFile)
		{
			printf("open file error\n");
			goto exit;
		}

		fwrite(pData, retval, 1, pFile);
		fclose(pFile);
		retval = pSession->Send((uint8 *)response, strlen(response));
		if(retval < 0)
		{
			printf("send error\n");
			goto exit;
		}
		printf("send size: %d\n", retval);
		pSession->Close();
		//pSession = NULL;
	}


exit:
	printf("Press any key...\n");
	getchar();
	return 0;
}

